//package com.ruoyi.quartz.task;
//
//import com.alibaba.fastjson.JSONObject;
//import com.ruoyi.bizsys.domain.SmsSendLog;
//import com.ruoyi.business.common.AsyncService;
//import com.ruoyi.common.utils.Func;
//import com.ruoyi.sms.mapper.SmsSendMapper;
//import com.ruoyi.system.service.ISysConfigService;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * 短信监控
// *
// * @author mac
// */
//@Slf4j
//@AllArgsConstructor
//@Component("smsMonitorTask")
//public class SmsMonitorTask {
//
//    private SmsSendMapper smsMapper;
//
////    private ISlackService iSlackService;
//
////    private IFeishuService iFeishuService;
//
//    private ISysConfigService iSysConfigService;
//
//    AsyncService asyncService;
//
//
//    public void earlyWarning() {
//        log.info("短信监控预警任务 Start >>>>>>>>>>");
//
//        //查询当天的短信任务
//        List<SmsSendLog> smsSendLogList = smsMapper.getSmsTaskSemih();
//        //当天短信发送条数>预警数量，进入预警机制
//        Integer sms_warning = Integer.valueOf(iSysConfigService.selectConfigByKey("sms_warning"));
//        if (smsSendLogList.size() > sms_warning) {
//            List<SmsSendLog> successList = smsSendLogList.stream().filter(e -> check(e)).collect(Collectors.toList());
//            //成功占比小于阈值
//            Double sms_success_threshold = Double.valueOf(iSysConfigService.selectConfigByKey("sms_success_threshold"));
//            if ((double) successList.size() / smsSendLogList.size() < sms_success_threshold) {
//
//                HashMap<String, Integer> allMap = new HashMap<>();
//                smsSendLogList.forEach(e -> {
//                    Integer integer = allMap.get(e.getSmsProvider());
//                    allMap.put(e.getSmsProvider(), integer == null ? 1 : ++integer);
//                });
//
//                HashMap<String, Integer> sucMap = new HashMap<>();
//                successList.forEach(e -> {
//                    Integer integer = sucMap.get(e.getSmsProvider());
//                    sucMap.put(e.getSmsProvider(), integer == null ? 1 : ++integer);
//                });
//
////                StringBuilder content = new StringBuilder();
////
////                content.append("[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"*短信发送成功比例低于阈值预警*\"}},");
////                content.append("{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"");
////
////                content.append("*`发送总数：`* " + smsSendLogList.size() + ";\\n");
////                content.append("*`成功总数：`* " + successList.size() + ";\\n");
//
//                JSONObject content = new JSONObject();
//                content.put("发送总数",smsSendLogList.size());
//                content.put("成功总数",successList.size());
//
//                for (String key : allMap.keySet()) {
//                    Integer allCount = Func.isNotEmpty(allMap.get(key)) ? allMap.get(key) : 0;
//                    Integer sucCount = Func.isNotEmpty(sucMap.get(key)) ? sucMap.get(key) : 0;
////                    content.append("*`" + key + "发送数量：`* " + allCount + ";\\n");
////                    content.append("*`" + key + "成功数量：`* " + sucCount + ";\\n");
//                    content.put(key+"发送数量",allCount);
//                    content.put(key+"成功数量",sucCount);
//                }
//
//
//
//
////                content.append("\"}}]");
////                //It-Test monitor-group
////                iSlackService.slackSendRobotMessage(content.toString(), "monitor-group");
//
//                //iFeishuService.sendMessageByName("monitor-group",content,"短信发送成功比例低于阈值预警");
//                asyncService.sendFeishu("monitor-group","短信发送成功比例低于阈值预警",content);
//            }
//        }
//    }
//
//    /**
//     * sendStatus=success，& updateTime - createTime < 5 min ,占总比 < 85%
//     *
//     * @param smsSendLog
//     * @return
//     */
//    private boolean check(SmsSendLog smsSendLog) {
//        long updateTime = smsSendLog.getUpdateTime().getTime();
//        long createTime = smsSendLog.getCreateTime().getTime();
//        double minuteDiff = (double) (updateTime - createTime) / (1000 * 60);
//        return minuteDiff < 5 && "success".equalsIgnoreCase(smsSendLog.getSendStatus());
//    }
//
//
//}
